<?php
/**
 * Inter组件之工厂：Session工厂类
 * 提供一个静态方法：快速初始化Session组件并返回具体的Inter_Session_Operator_Abstract。
 * 例子：
 * <code>
 * $sess = Inter_Factory_Session::produce();
 * //如果你将Inter_Factory_Session的配置“AUTO_START”设置为false，就要自行session_start()
 * if(!Inter_Factory_Session::isAutoStart()){
 *     $sess->session_start();
 * }
 * $sess->set('login', 1);
 * </code>
 *
 * @author Horse Luke<horseluke@126.com>
 * @copyright Horse Luke, 2010
 * @license the Apache License, Version 2.0 (the "License"). {@link http://www.apache.org/licenses/LICENSE-2.0}
 * @version $Id: Session.php 153 2011-02-04 09:48:39Z horseluke@126.com $
 * @package Inter_Session
 * @subpackage Inter_Factory_Session
 */
class Inter_Factory_Session{
    
    /**
     * @var Inter_Session_Operator_Abstract
     */
    protected static $_operator = null;
    
    /**
     * @var array
     */
    protected static $_config = array();
    
    /**
     * 该类禁止实例化，请使用{@link Inter_Factory_Session::produce()}
     */
    private function __construct(){
        
    }
    
    /**
     * 生产并返回一个具体实例：Inter_Session_Operator_Native或者Inter_Session_Operator_Simulator
     * @see Inter_Session_Operator_Native、Inter_Session_Operator_Simulator
     * @return Inter_Session_Operator_Abstract
     */
    public static function produce(){
        if(null === self::$_operator){
            self::_init();
        }
        return self::$_operator;
    }
    
    /**
     * 查询session是否已经自动启动了？
     */
    public static function isAutoStart(){
        return true == self::$_config['session.auto_start'];
    }
    
    /**
     * 查询使用了什么session引擎？
     */
    public static function getEngine(){
        return array(
                'OPERATOR_TYPE' => self::$_config['OPERATOR_TYPE'], 
                'STORAGE_TYPE' => self::$_config['STORAGE_TYPE'], 
        );
    }
    
    /**
     * 初始化所有资源
     */
    protected static function _init(){
        self::$_config = require dirname(__FILE__). '/../../../Config/'. __CLASS__. '.php';
        self::_initOperator();
    }
    
    /**
     * 初始化静态属性：{@link Inter_Factory_Session::_operator}
     */
    protected static function _initOperator(){
        $operatorClassName = 'Inter_Session_Operator_'. self::$_config['OPERATOR_TYPE'];
        $storageClassName = empty(self::$_config['STORAGE_TYPE']) ? '' : 'Inter_Session_Storage_'. self::$_config['STORAGE_TYPE'];
        
        //session操作器初始化
        self::$_operator = new $operatorClassName();
        
        //session存储器注册到session操作器中
        if( !empty($storageClassName) ){
            $sessStorage = new $storageClassName();
            self::$_operator->setStorageHandler($sessStorage);
        //模拟操作器必须要有一个session存储器
        }elseif( 'Simulator' == self::$_config['OPERATOR_TYPE'] ){
            throw new Exception('You have defined Simulator session OPERATOR but does not define a session STORAGE type! SYSTEM HALTED!');
        }
        
        if(true == self::$_config['session.auto_start']){
            self::$_operator->session_start();
        }
    }
    
    
    
    
}